package com.kingdee.eas.custom.fi.arap.app;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import javax.sql.RowSet;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.dao.query.ISQLExecutor;
import com.kingdee.bos.dao.query.SQLExecutorFactory;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.eas.basedata.assistant.CurrencyFactory;
import com.kingdee.eas.basedata.assistant.CurrencyInfo;
import com.kingdee.eas.basedata.assistant.PeriodInfo;
import com.kingdee.eas.basedata.master.auxacct.AsstActTypeInfo;
import com.kingdee.eas.basedata.org.CompanyOrgUnitInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.fi.ap.OtherBillInfo;
import com.kingdee.eas.fi.ar.ArApBillBaseInfo;
import com.kingdee.eas.fi.ar.ImportException;
import com.kingdee.eas.fi.ar.app.util.ArServerUtils;
import com.kingdee.eas.fi.arap.app.util.ArApServerUtils;
import com.kingdee.eas.fi.gl.VoucherInfo;
import com.kingdee.eas.tools.datatask.core.TaskExternalException;
import com.kingdee.eas.util.ResourceBase;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.jdbc.rowset.IRowSet;

public class OtherBillImporter  {


	   protected  EntityViewInfo getEntityViewByNum(String number)
	    {
		   
	        EntityViewInfo entityView = new EntityViewInfo();
	        FilterInfo filter = new FilterInfo();
	        filter.getFilterItems().add(new FilterItemInfo("number", number));
	        entityView.setFilter(filter);
	        return entityView;
	    }
	   
	   

	   protected  EntityViewInfo getGeneralTypeByLonNumber(String number)
	    {
		   
		    if(number!=null){
		    	 number =number.trim().replace('.', '!');	
		    }
		    EntityViewInfo entityView = new EntityViewInfo();
		     FilterInfo filter = new FilterInfo();
		     filter.getFilterItems().add(new FilterItemInfo("longNumber", number));
		     entityView.setFilter(filter);
		     return entityView;
		   
	      
	    }
	  
	
	//private static final long serialVersionUID = 8082151450168565254L;
	public static final String AR_PLAN_DECIMAL_FIELDS[] = {
	        "FRecievePayAmount_RR", "FRecievePayAmountLocal_RR", "FVerifyAmount_RR", "FVerifyAmountLocal_RR"
	    };
	    public static final String AR_PLAN_DECIMAL_OBJS[] = {
	        "recievePayAmount", "recievePayAmountLocal", "verifyAmount", "verifyAmountLocal"
	    };
	    public static final String AP_PLAN_DECIMAL_FIELDS[] = {
	        "FRecievePayAmount_PP", "FRecievePayAmountLocal_PP", "FVerifyAmount_PP", "FVerifyAmountLocal_PP"
	    };
	    public static final String AP_PLAN_DECIMAL_OBJS[] = {
	        "recievePayAmount", "recievePayAmountLocal", "verifyAmount", "verifyAmountLocal"
	    };
	    protected CurrencyInfo baseCurrency;
	    protected CompanyOrgUnitInfo currentCompany;
	    protected PeriodInfo startPeriod;
	    protected PeriodInfo currentPeriod;
	    protected boolean isInitBill;
	    protected boolean arIsStart;
	    protected boolean isFinish;
	    protected boolean isConnSCM;
	    protected BigDecimal amountVal;
	    protected BigDecimal amountLocalVal;
	    protected BigDecimal exchangeRateVal;
	    
	    public void   importData(Context ctx, Hashtable hsData, ArrayList headFields, ArrayList entryFields)throws  TaskExternalException {
	    	  
		 } 
	    
	    
	   public OtherBillImporter(Context ctx){
		   
		   currentCompany = ContextUtil.getCurrentFIUnit(ctx);
		try {
			if (currentCompany.getBaseCurrency() != null)
				 baseCurrency = CurrencyFactory.getLocalInstance(ctx).getCurrencyInfo(new ObjectUuidPK(currentCompany.getBaseCurrency().getId()));
			else
				throw new ImportException(ImportException.CANNOTGETLOCALCURRENCY);

			startPeriod = ArServerUtils.getArStartPeriod(ctx, currentCompany);
			arIsStart = ArServerUtils.arIsStart(ctx, currentCompany);
			currentPeriod = ArServerUtils.getArCurrentPeriod(ctx,currentCompany);
			isConnSCM = ArApServerUtils.isARRelationSCM(ctx, currentCompany.getId().toString());
		} catch (EASBizException e) {

			e.printStackTrace();
		} catch (BOSException e) {
		
			e.printStackTrace();
		}
             
	   }
	    
	   public static String getString(String resName, Context ctx)
	   {
	       return getString("AR", resName, ctx);
	   }

	   public static String getString(String SysType, String resName, Context ctx)
	   {
	       String AR_RESOURCE_PATH = "com.kingdee.eas.fi.ar.ARImportData";
	       String AP_RESOURCE_PATH = "com.kingdee.eas.fi.ap.APImportData";
	       if(SysType.equals("AR"))
	           return ResourceBase.getString(AR_RESOURCE_PATH, resName, ctx.getLocale());
	       if(SysType.equals("AP"))
	           return ResourceBase.getString(AP_RESOURCE_PATH, resName, ctx.getLocale());
	       else
	           return null;
	   } 
	   
	   protected void setCheckField(String fieldName, BigDecimal value, Context ctx)
	   throws TaskExternalException
	{
	   if(fieldName.trim().equals("FAmount"))
	   {
	       if(value == null || value.compareTo(ARAPImportHelper.ZERO) == 0)
	           throw new TaskExternalException("FAmount is empty!");
	       amountVal = value;
	   } else
	   if(fieldName.trim().equals("FAmountLocal"))
	   {
	       if(value == null || value.compareTo(ARAPImportHelper.ZERO) == 0)
	           throw new TaskExternalException("FAmountLocal is empty!");
	       amountLocalVal = value;
	   } else
	   if(fieldName.trim().equals("FExchangeRate"))
	   {
	       if(value == null || value.compareTo(ARAPImportHelper.ZERO) == 0)
	           throw new TaskExternalException(getString("FExchangeRate_IsNull", ctx));
	       exchangeRateVal = value.compareTo(ARAPImportHelper.ZERO) != 0 ? value : ARAPImportHelper.ONE;
	   }
	}
	 
	   
    protected boolean checkBillDate(ArApBillBaseInfo otherBillInfo, Context ctx)
       throws TaskExternalException
   {
       boolean ret = true;
       Date bizDate = otherBillInfo.getBizDate();
       if(bizDate == null)
           throw new TaskExternalException(getString("BizDateIsNull", ctx));
       Date billDate = otherBillInfo.getBillDate();
       if(billDate.before(bizDate))
           ret = false;
       return ret;
   }
    
    
  protected boolean checkAsstAccID(Context ctx, AsstActTypeInfo asstActTypeInfo, Hashtable hsData,  ArApBillBaseInfo otherBillInfo)
    throws TaskExternalException
{
    boolean flag = false;
    String str = ARAPImportHelper.getDataString(hsData, "FAsstActNumber");
    if(str == null)
        throw new TaskExternalException(getString("FAsstActNumber_IsNull", ctx));
    String fId = null;
    String fName = null;
    if(str != null && asstActTypeInfo != null && asstActTypeInfo.getDefaultQueryName() != null)
    {
        String tableName = asstActTypeInfo.getRealtionDataObject();
        if(tableName != null && tableName.length() > 0)
            try
            {
                for(RowSet rs = ARAPImportHelper.getAsstActByNumber(ctx, tableName, str); rs.next();)
                {
                    flag = true;
                    fId = rs.getString("fid");
                    fName = rs.getString("fname");
                }

            }
            catch(Exception e)
            {
                throw new TaskExternalException(e.getMessage(), e);
            }
    }
    otherBillInfo.setAsstActID(fId);
    otherBillInfo.setAsstActName(fName);
    return flag;
}
	    
}
